//
//  BaseTabbarViewController.m
//  QA
//
//  Created by Yoga on 2017/4/19.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "BaseTabbarViewController.h"
#import "BaseNavigationViewController.h"
#import "MineTabbar.h"

@interface BaseTabbarViewController ()<UITabBarControllerDelegate, MineTabbarDelegate>

@property (nonatomic, strong) BaseNavigationViewController *first;
@property (nonatomic, strong) BaseNavigationViewController *second;
@property (nonatomic, strong) BaseNavigationViewController *third;
@property (nonatomic, strong) BaseNavigationViewController *fourth;
@property (nonatomic, strong) BaseNavigationViewController *fifth;

@property (nonatomic, strong) UILabel *badgeLabel;
@end

@implementation BaseTabbarViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(observeShowBadge:) name:ReciveRongCloudMessage object:nil];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initViewControllers];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.badgeLabel.frame = CGRectMake(ScreenWidth * 0.9 + 5, 3, self.badgeLabel.width, self.badgeLabel.height);
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ReciveRongCloudMessage object:nil];
}
#pragma mark - method
- (void)initViewControllers {
//    MineTabbar *tabBar = [[MineTabbar alloc] init];
//    tabBar.tabBarDelegate = self;
//    [self setValue:tabBar forKeyPath:@"tabBar"];
    
    self.delegate = self;
    self.viewControllers = @[self.first, self.second, self.third, self.fourth, self.fifth];
    self.selectedIndex = 0;
    
    [UITabBar appearance].translucent = NO;
    [[UITabBar appearance] setBarTintColor:[UIColor colorWithHex:0xF8F8F8 alpha:1.0]];
    
    [self.tabBar addSubview:self.badgeLabel];
    
    [self updateBadge:[UserInfo shareInstance].user.showBadge];
}

- (void)observeShowBadge:(id)sender {
    [self updateBadge:YES];
}

- (void)updateBadge:(BOOL)isShowBadge {
    if (isShowBadge) {
        self.badgeLabel.hidden = NO;
//        self.fourth.tabBarItem.badgeValue = @"";
    } else {
        self.badgeLabel.hidden = YES;
//        self.fourth.tabBarItem.badgeValue = nil;
    }
}

#pragma mark - MineTabbarDelegate
- (void)tabBarMidButtonClick:(MineTabbar *)tabBar {
    if (![UserInfo shareInstance].isLogin) {
//        LoginViewController *loginVC = [[LoginViewController alloc] init];
//        BaseNavigationViewController *nav = [[BaseNavigationViewController alloc] initWithRootViewController:loginVC];
//        [self presentViewController:nav animated:YES completion:nil];
        return;
    }
    
//    if (![UserInfo shareInstance].user.isshop) {
//        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您暂未开启店铺，是否去开店铺?" preferredStyle:UIAlertControllerStyleAlert];
//        __weak typeof(self) weakSelf = self;
//        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//
//        }];
//        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"去开店" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            [weakSelf setSelectedIndex:2];
//        }];
//        [alertController addAction:cancelAction];
//        [alertController addAction:sureAction];
//        [self presentViewController:alertController animated:YES completion:nil];
//
//        return;
//    }
//
//    PublishViewController *publishVC = [[PublishViewController alloc] init];
//
//    BaseNavigationViewController *baseNavigationVC = [[BaseNavigationViewController alloc] initWithRootViewController:publishVC];
//
//    [self presentViewController:baseNavigationVC animated:YES completion:^{
//
//    }];
}

#pragma mark - UITabBarDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
//    NSUInteger indexOfTab = [tabBarController.viewControllers indexOfObject:viewController];
    
    return YES;
}

#pragma mark - getters
- (BaseNavigationViewController *)first {
    if (!_first) {
        _homeVC = [[HomeViewController alloc] init];
        
        _first = [[BaseNavigationViewController alloc] initWithRootViewController:_homeVC];
        
        _first.tabBarItem.title = @"首页";
        [_first.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x5D5F6A alpha:1.0]} forState:UIControlStateNormal];
        [_first.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor ThemeColor]} forState:UIControlStateSelected];
        _first.tabBarItem.image = [[UIImage imageNamed:@"home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        _first.tabBarItem.selectedImage = [[UIImage imageNamed:@"home_red"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    return _first;
}

- (BaseNavigationViewController *)second {
    if (!_second) {
        _classifyVC = [[ClassifyViewController alloc] init];
        
        _second = [[BaseNavigationViewController alloc] initWithRootViewController:_classifyVC];
        
        _second.tabBarItem.title = @"分类";
        [_second.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x5D5F6A alpha:1.0]} forState:UIControlStateNormal];
        [_second.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor ThemeColor]} forState:UIControlStateSelected];
        _second.tabBarItem.image = [[UIImage imageNamed:@"class"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        _second.tabBarItem.selectedImage = [[UIImage imageNamed:@"class_red"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    return _second;
}

- (BaseNavigationViewController *)third {
    if (!_third) {
        _discoverVC = [[DiscoverViewController alloc] init];
        
        _third = [[BaseNavigationViewController alloc] initWithRootViewController:_discoverVC];
        _third.tabBarItem.title = @"发现";
        
        [_third.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x5D5F6A alpha:1.0]} forState:UIControlStateNormal];
        [_third.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor ThemeColor]} forState:UIControlStateSelected];
        _third.tabBarItem.image = [[UIImage imageNamed:@"fand"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        _third.tabBarItem.selectedImage = [[UIImage imageNamed:@"fand_red"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    return _third;
}

- (BaseNavigationViewController *)fourth {
    if (!_fourth) {
        _cartVC = [[ShoppingCartHomeViewController alloc] init];
        
        _fourth = [[BaseNavigationViewController alloc] initWithRootViewController:_cartVC];
        _fourth.tabBarItem.title = @"购物车";
        
        [_fourth.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x5D5F6A alpha:1.0]} forState:UIControlStateNormal];
        [_fourth.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor ThemeColor]} forState:UIControlStateSelected];
        _fourth.tabBarItem.image = [[UIImage imageNamed:@"shop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        _fourth.tabBarItem.selectedImage = [[UIImage imageNamed:@"shop_red"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    return _fourth;
}

- (BaseNavigationViewController *)fifth {
    if (!_fifth) {
        _mineVC = [[MineViewController alloc] init];
        
        _fifth = [[BaseNavigationViewController alloc] initWithRootViewController:_mineVC];
        _fifth.tabBarItem.title = @"我的";
        
        [_fifth.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x5D5F6A alpha:1.0]} forState:UIControlStateNormal];
        [_fifth.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor ThemeColor]} forState:UIControlStateSelected];
        _fifth.tabBarItem.image = [[UIImage imageNamed:@"mine"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        _fifth.tabBarItem.selectedImage = [[UIImage imageNamed:@"mine_red"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    return _fifth;
}

- (UILabel *)badgeLabel {
    if (!_badgeLabel) {
        _badgeLabel = [[UILabel alloc] init];
        _badgeLabel.backgroundColor = [UIColor ThemeColor];
        _badgeLabel.size = CGSizeMake(8, 8);
        _badgeLabel.layer.cornerRadius = _badgeLabel.height * 0.5;
        _badgeLabel.clipsToBounds = YES;
        _badgeLabel.hidden = YES;
    }
    return _badgeLabel;
}

@end
